package com.cqggrw.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.cqggrw.dao.Category;
import com.cqggrw.dao.NewsDaoI;
import com.cqggrw.dao.impl.NewsDaoImpl;
import com.cqggrw.domain.Img;
import com.cqggrw.domain.News;
import com.cqggrw.service.NewsServiceI;
import com.cqggrw.util.Utils;

public class NewsServiceImpl implements NewsServiceI<News> {

	private NewsDaoI<News> dao = new NewsDaoImpl();

	public boolean add(News t, String realPath) {
		String falg = UUID.randomUUID().toString();
		try {
			for (int i = 0; t.getImg() != null && i < t.getImg().length; i++) {
				Img img = new Img();
				img.setName(UUID.randomUUID().toString() + "."
						+ t.getImgFileName()[i].replace(".", ",").split(",")[1]);
				Utils.upFile(img.getName(), realPath, t.getImg()[i]);
				img.setFalg(falg);
				if (t.getDes() != null) {
					img.setDes(t.getDes()[i]);
				}
				if (i == 0) {
					t.setFilename(img.getName());
				}
				dao.addImg(img);
			}
			t.setFalg(falg);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return dao.add(t);
	}

	public boolean del(int id) {
		return dao.del(id);
	}

	public boolean modify(News t) {
		return dao.modify(t);
	}

	public News get(News t) {
		return dao.get(t);
	}

	public Object get() {
		List<News> list = dao.get();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", 100);
		map.put("rows", list);
		return map;
	}

	public List<News> getByTypeId(int id) {
		if(!Utils.closeApp) {
			return dao.getByTypeId(id);
		}else {
			return null;
		}
	}

	public Object get(News t, int i) {
		List<News> list = dao.get(t, i);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", dao.getTotal(t));
		map.put("rows", list);
		return map;
	}

	public List<News> searchByName(String name, Integer page) {
		return dao.searchByName(name, page);
	}

	public int countForSearch(String name) {
		return dao.countForSearch(name);
	}

	public List<Category> getCate() {
		return dao.getCate();
	}

	public List<Img> getImgByFalg(News news) {
		return dao.getNewsImg(news.getFalg());
	}

	public boolean addNewsImg(News t, String realPath) {
		boolean falg = true;
		try {
			Img img = new Img();
			img.setName(UUID.randomUUID().toString() + "."
					+ t.getImgFileName()[0].replace(".", ",").split(",")[1]);
			Utils.upFile(img.getName(), realPath, t.getImg()[0]);
			if (t.getDes() != null) {
				img.setDes(t.getDes()[0]);
			}
			img.setFalg(t.getFalg());
			dao.addImg(img);
			if(t.getImgCount()==0) {
				t.setFilename(img.getName());
				t.setFileDes(t.getDes()[0]);
				dao.updateNewsImg(t);
			}
		} catch (Exception e) {
			falg = false;
			Utils.log(e);
		}
		return falg;
	}

	public boolean updateNewsImg(News t, String realPath) {
		boolean falg = true;
		if(t.getDes()!=null) {
			t.setFileDes(t.getDes()[0]);
		}
		try {
			Img img = new Img();
			if(t.getImg()!=null) {
				img.setName(UUID.randomUUID().toString() + "."
						+ t.getImgFileName()[0].replace(".", ",").split(",")[1]);
				Utils.upFile(img.getName(), realPath, t.getImg()[0]);
			} else {
				img.setName(t.getFilename());
			}
			img.setDes(t.getFileDes());
			img.setId(t.getId());
			dao.modifyImg(img);
			if(t.getImgCount()==1) {
				t.setFilename(img.getName());
				t.setFileDes(t.getFileDes());
				t.setId(t.getDi());
				dao.updateNewsImg(t);
			}
		} catch (Exception e) {
			falg = false;
			Utils.log(e);
		}
		return falg;
	}

	public boolean delNewsImg(News t) {
		boolean falg = true;
		
		dao.delNewsImg(t.getId());
		
		List<Img> imgList = dao.getNewsImg(t.getFalg());
		
		if(t.getImgCount() == 1) {
			t.setFileDes("");
			t.setFilename("");
			if(imgList.size()>0) {
				t.setFileDes(imgList.get(0).getDes());
				t.setFilename(imgList.get(0).getName());
			}
			
			t.setId(t.getDi());
			dao.updateNewsImg(t);
		}
		
		return falg;
	}

	public List<News> getListHasImgByType(Integer id) {
		return dao.getListHasImgByType(id);
	}

}
